<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Inv_roles_model extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
    }

    public function get_inv_roles(){
      $sql   = "SELECT * FROM `app_lookup_values` WHERE lookup_type = 'INV_ROLES'";
      $query = $this->db->query($sql);
      return $query->result();
    }

    public function get_inv_users($id){
      $sql    = "SELECT * FROM app_users WHERE enable_flag = 'Y' and company_id = ".$id." ";
      $query  = $this->db->query($sql);
      return $query->result();
    }

    public function get_offcr_data($id){
      $sql    = "SELECT io.*,au.user_name,au.first_name,au.last_name FROM inv_officers io 
                      INNER JOIN app_users au on au.user_id = io.user_id
                      WHERE inv_role_id =".$id." ";
      $query  = $this->db->query($sql);
      return $query->result();
    }

    public function get_userinfo($id){
      $sql = "SELECT * from app_users where user_id = ".$id." and company_id = ".$this->session->userdata('company_id')." " ;
      $query = $this->db->query($sql);
      echo json_encode($query->result());
    }

    public function insert_offcr($data){
      $this->db->query("call inv_officer_ins(?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }

    public function update_offcr($data){
      $this->db->query("call inv_officer_upd(?,?,?,?,@err,@msg)",$data);
        $que = $this->db->query("SELECT @err as error , @msg as msg");
        echo json_encode($que->result());
    }

    public function search_offcr($uname, $role_code){
      $where = "";
      

          if($uname != "" && $role_code != ""){
            $where = "WHERE au.user_name LIKE ".$this->db->escape($uname)." AND io.role_code LIKE '".$role_code."' ";
          }else if($uname == "" && $role_code != ""){
            $where = "WHERE io.role_code LIKE ".$this->db->escape($role_code)." ";
          }else{
            $where = "WHERE au.user_name LIKE ".$this->db->escape($uname)." ";
          }

          $sql = "SELECT io.* , au.user_name , au.first_name , au.last_name , alv.meaning FROM inv_officers io 
                INNER JOIN app_users au ON au.user_id = io.user_id 
                INNER JOIN app_lookup_values alv on alv.lookup_code = io.role_code ".$where." ";
          //$query = $this->db->get_where('inv_uom', array('module_code' => $modulecode, 'module_name' => $modulename), 1);
          $query = $this->db->query($sql);
          if($query->num_rows() != 0){
            return $query->result();
          }
          else {
            $result = "No results found.";
            return $result;
          }
    }



}